//
//  GoodArticalViewController.m
//  jinnengExam
//
//  Created by mac on 2020/9/10.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "GoodArticalViewController.h"
#import "NewsDetailViewController.h"
#import "VideoDetailViewController.h"
#import "TopicVideoViewController.h"

@interface GoodArticalViewController ()<WKNavigationDelegate>
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic) int page;
@property(nonatomic,strong) NSString *module;
@property(nonatomic,strong) NSString * trade;
@end

@implementation GoodArticalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _module = @"[6,16]";
    _trade = @"";
    [self initView];
    [self registerNativeFunctions];
}
-(void) viewWillAppear:(BOOL)animated{
    [self.jznWebView.scrollView.mj_header beginRefreshing];
}
-(void) initView{
   [self.view addSubview:self.jznWebView];
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-40);
        } else {
            make.top.bottom.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-40);
        }
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/videoClass" type:@"html"];
    
//    JSContext *context = [self.jznWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    context[@"moduleChange"] = ^(){
//        NSArray *args = [JSContext currentArguments];
//        NSString *moduleString = [args[0] toString];
//        self.module = moduleString;
//        [self.jznWebView.scrollView.mj_header beginRefreshing];
//    };
//
//    context[@"tradeChange"] = ^(){
//           NSArray *args = [JSContext currentArguments];
//           NSString *tradeString = [args[0] toString];
//            self.trade = tradeString;
//           [self.jznWebView.scrollView.mj_header beginRefreshing];
//       };
//
//    context[@"toNewsDetailViewController"]= ^(){
//        NSArray *args = [JSContext currentArguments];
//        NSString *params = [args[0] toString];
//        dispatch_async(dispatch_get_main_queue(), ^(){
//            NewsDetailViewController *newsDetailViewController = [[NewsDetailViewController alloc]init];
//            [newsDetailViewController setParams:params];
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:newsDetailViewController animated:YES];
//        });
//    };
//
//
//    context[@"toVideoDetailViewController"] = ^(){
//            NSArray *args = [JSContext currentArguments];
//            NSString *dataId = [args[0] toString];
//            NSString *cover = [args[1] toString];
//            dispatch_async(dispatch_get_main_queue(), ^(){
//                VideoDetailViewController *vc = [[VideoDetailViewController alloc]init];
//                vc.videoNewsId = dataId;
//                vc.imageUrl = cover;
//                [self.navigationController pushViewController:vc animated:YES];
//            });
//    };
//
//    context[@"toTopicDetailViewController"] = ^(){
//            NSArray *args = [JSContext currentArguments];
//            NSString *dataId = [args[0] toString];
//
//            dispatch_async(dispatch_get_main_queue(), ^(){
//                TopicVideoViewController *vc = [[TopicVideoViewController alloc]init];
//                vc.topicId = dataId;
//                [self.navigationController pushViewController:vc animated:YES];
//            });
//    };
//
    [self.jznWebView.scrollView.mj_header setBackgroundColor:[UIColor whiteColor]];
    self.jznWebView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self requestLearn:self.page];
        
    }];
    
    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestLearn:++self.page];
    }];
}


- (void)registerNativeFunctions {
    
    [self.webViewBridge registerHandler:@"tradeChange" handler:^(id data, WVJBResponseCallback responseCallback) {
        self.trade = data[@"module"];
        [self.jznWebView.scrollView.mj_header beginRefreshing];
    }];
    
    
    [self.webViewBridge registerHandler:@"toNewsDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {

        dispatch_async(dispatch_get_main_queue(), ^(){
            NewsDetailViewController *newsDetailViewController = [[NewsDetailViewController alloc]init];
            [newsDetailViewController setParams:[data mj_JSONString]];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:newsDetailViewController animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toVideoDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {
                    
            NSString *dataId =data[@"dataId"];
            NSString *cover = data[@"coverUrl"];
            dispatch_async(dispatch_get_main_queue(), ^(){
                VideoDetailViewController *vc = [[VideoDetailViewController alloc]init];
                vc.videoNewsId = dataId;
                vc.imageUrl = cover;
                [self.navigationController pushViewController:vc animated:YES];
            });
    }];
    
    [self.webViewBridge registerHandler:@"toTopicDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {
           NSString *dataId = data[@"dataId"];

            dispatch_async(dispatch_get_main_queue(), ^(){
                TopicVideoViewController *vc = [[TopicVideoViewController alloc]init];
                vc.topicId = dataId;
                [self.navigationController pushViewController:vc animated:YES];
            });
    }];
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
   [self.jznWebView.scrollView.mj_header beginRefreshing];
}

-(void) requestLearn:(int) page{
    NSString *url = [NSString stringWithFormat:@"%@/newsView/findAppListData" ,[URLUtil hostName]];
    NSDictionary *params = @{@"module":_module, @"trade":_trade,@"pageNum":[NSString stringWithFormat:@"%d",page],@"pageSize":@"5"};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            if (page == 1) {
                NSString *clear = @"clear()";
               
                [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                    
                }];
                [self.jznWebView.scrollView.mj_header endRefreshing];
                [self.jznWebView.scrollView.mj_footer resetNoMoreData];
            }
            
            NSString *clear = [NSString stringWithFormat:@"getNewsList(%@)",[dataResult.data mj_JSONString]];
           // [self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
            
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
            NSArray *array = dataResult.data;
            if([array count]<5){
                
                [self.jznWebView.scrollView.mj_footer endRefreshingWithNoMoreData];
            }else{
                
                [self.jznWebView.scrollView.mj_footer endRefreshing];
            }
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
